package com.intelligent.mts.da.handler;

import com.intelligent.mts.da.xmlmessage.HttpXmlResponse;
import com.intelligent.mts.da.utils.JaxbUtil;
import com.intelligent.mts.da.xmlmessage.TestReturnMsg;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.http.FullHttpResponse;

import java.nio.charset.Charset;
import java.util.List;

/**
 * Created by Garen.Pang on 2016/9/5.
 */
public class HttpXmlRespsonseDecoder2010 extends MessageToMessageDecoder<FullHttpResponse> {
    private final static String CHARSET_NAME = "UTF-8";
    private final static Charset UTF_8 = Charset.forName(CHARSET_NAME);


    @Override
    protected void decode(ChannelHandlerContext ctx, FullHttpResponse msg, List<Object> out) throws Exception {
        HttpXmlResponse resHttpXmlResponse = new HttpXmlResponse(msg, decode0(ctx, msg.content()));
        out.add(resHttpXmlResponse);
    }

    protected Object decode0(ChannelHandlerContext ctx, ByteBuf body)
            throws Exception {

        String content = body.toString(UTF_8);
        System.out.println("Receive msg from sever xml:\n"+content);

        TestReturnMsg TestReturnMsg = JaxbUtil.converyToJavaBean(content, TestReturnMsg.class);

        return TestReturnMsg;
    }
}
